package com.ruoyi.project.module.shao;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.library.api.ISysDictDataService;
import com.ruoyi.library.api.domain.SysDictData;
import com.ruoyi.project.domain.HeEventFlow;
import com.ruoyi.project.domain.HeEventReport;
import com.ruoyi.project.service.IHeEventFlowService;
import com.ruoyi.project.service.IHeEventReportService;
import com.ruoyi.project.service.ISysUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 事件上报信息Controller
 *
 * @author ruoyi
 * @date 2023-09-02
 */
@RestController
@RequestMapping("/shao/report")
public class ShaoshijianReportController extends BaseController {
    @Autowired
    private IHeEventReportService heEventReportService;
    @Autowired
    private IHeEventFlowService heEventFlowService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询事件上报信息列表
     */
//    @RequiresPermissions("project:report:list")
    @GetMapping("/list")
    public TableDataInfo list(HeEventReport heEventReport) {
        List<HeEventReport> list = heEventReportService.selectHeEventReportList(heEventReport);
        return getDataTable(list);
    }

    /**
     * 获取事件上报信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(heEventReportService.selectHeEventReportById(id));
    }

    /**
     * 新增事件上报信息
     */
    @Log(title = "事件上报信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HeEventReport heEventReport) {
        return toAjax(heEventReportService.insertHeEventReport(heEventReport));
    }

    //调用字典
    @GetMapping("/deptlist")
    public TableDataInfo deptList(HeEventReport heEventReport) {
        startPage();
        List<HeEventReport> list = heEventReportService.selectHeEventReportListBySupportDepartment(heEventReport);
        SysDictData dictData = new SysDictData();
        List<SysDictData> list1 = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }


    /**
     * 改派事件接口
     * 本质就是原生修改接口
     */
    @Log(title = "事件上报信息", businessType = BusinessType.UPDATE)
    @PutMapping("/gaipai")
    public AjaxResult gai(@RequestBody HeEventReport heEventReport)
    {
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        //新建字典对象
        SysDictData dictData = new SysDictData();
        //将字典对象的查询条件赋死为科室部门的字典类型这样防止数字重复问题造成的显示bug
        dictData.setDictType("he_department_name");
        dictData.setDictValue(heEventReport.getReviewDepartment());
        List<SysDictData> list1 = dictDataService.selectDictDataList(dictData);
        //字符串拼接登录用户呈送了事件到某某科 某某科就是字典内容
        String pin = sysUser.getNickName() + "改派了事件到" + list1.get(0).getDictLabel();
        //新建个流转表对象
        HeEventFlow heEventFlow = new HeEventFlow();
        //将report表查询的id注入到flow表的reporteventid中
        heEventFlow.setReportEventId(heEventReport.getId());
        heEventFlow.setCreationTime(DateUtils.getNowDate());
        heEventFlow.setProcessStreamContent(pin);
        heEventFlowService.insertHeEventFlow(heEventFlow);
        return toAjax(heEventReportService.updateHeEventReport(heEventReport));
    }
    /**
     * 呈送事件
     */
    @PutMapping
    public AjaxResult edit(@RequestBody HeEventReport heEventReport) {
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();// 当前用户

/*        SysUser User = new SysUser(); // 呈送科室的主任用户
        User.setDeptId(Long.valueOf(heEventReport.getReviewDepartment()));
        List<SysUser> userList=userService.selectUserListByDeptId(User);
        if(userList.size()>0){
            User=userList.get(0);
        }else{
            return  new AjaxResult(500,"您呈送的科室暂无主任，请联系信息科分配！");
        }*/

        SysUser User2 = new SysUser(); // 本科室的审核用户
        User2.setDeptId(sysUser.getDeptId());
        List<SysUser> userList2=userService.selectUserListByDeptId(User2);
        if(!userList2.isEmpty()){
            User2 = userList2.get(0);
            System.out.println("本科室的审核用户:" + User2.getUserName());
        }else{
            return  new AjaxResult(500,"您当前的科室暂无审核员，请联系信息科分配！");
        }

        SysUser User1 = new SysUser(); // 呈送科室的处理用户
        User1.setDeptId(Long.valueOf(heEventReport.getReviewDepartment()));
        List<SysUser> userList1=userService.selectUserListByDeptId(User1);
        if(!userList1.isEmpty()){
            User1 = userList1.get(0);
            System.out.println("呈送科室的处理用户:" + User1.getUserName());
        }else{
            return  new AjaxResult(500,"您呈送的科室暂无处理员，请联系信息科分配！");
        }

        //修改事件上报表
        //修改事件上报状态为待审核
        heEventReport.setReportEventState("02");
        //修改事件审核状态为未审
        heEventReport.setReviewStatus("03");
        heEventReport.setReviewAdvice("");
        //新建字典对象
        SysDictData dictData = new SysDictData();
        //将字典对象的查询条件赋死为科室部门的字典类型这样防止数字重复问题造成的显示bug
        dictData.setDictType("he_department_name");
        dictData.setDictValue(heEventReport.getReviewDepartment());
        List<SysDictData> list1 = dictDataService.selectDictDataList(dictData);
        //获取当前时间
        heEventReport.setCreationTime(DateUtils.getNowDate());
        //字符串拼接登录用户呈送了事件到某某科 某某科就是字典内容
        String pin ;
        //前一个事件
        HeEventFlow eventFlow = new HeEventFlow();
        if(heEventReport.getNote10().equals("02")){
            eventFlow.setReportEventId(heEventReport.getId());
            List<HeEventFlow>   list= heEventFlowService.selectHeEventFlowList(eventFlow);
            if(list.size()>0){
                eventFlow=list.get(0);
                //固定部分要保留的部分
                String originalString = eventFlow.getProcessStreamContent();
                //固定部分
                String fixedPart = sysUser.getNickName();

                String result = originalString.replace(fixedPart, "");
                String flow="匿名"+result;
                eventFlow.setProcessStreamContent(flow);

                heEventFlowService.updateHeEventFlow(eventFlow);
            }

            //非匿名上报
            heEventReport.setUserId(sysUser.getUserId());
            heEventReport.setReportFillerId(0L);
            heEventReport.setReportFiller("匿名");
            pin = "匿名" + "呈送了事件到" + list1.get(0).getDictLabel()+"。"
                    +"\n填报部门审核员："+User2.getNickName()+"(工号："+User2.getUserName()+")。"
                    +"\n呈送部门处理员："+User1.getNickName()+"(工号："+User1.getUserName()+")。";
        }
        else{
            pin = sysUser.getNickName() + "呈送了事件到" + list1.get(0).getDictLabel()+"。"
                    +"\n填报部门审核员："+User2.getNickName()+"(工号："+User2.getUserName()+")。"
                    +"\n呈送部门处理员："+User1.getNickName()+"(工号："+User1.getUserName()+")。";
            heEventReport.setUserId(sysUser.getUserId());
        }

        heEventReport.setNote10(null);
        //新建个流转表对象
        HeEventFlow heEventFlow = new HeEventFlow();


        //将分析表的reporteventid注入到flow表的reporteventid中
        heEventFlow.setReportEventId(heEventReport.getId());
        heEventFlow.setCreationTime(DateUtils.getNowDate());
        heEventFlow.setProcessStreamContent(pin);
        heEventFlowService.insertHeEventFlow(heEventFlow);
        return toAjax(heEventReportService.updateHeEventReport(heEventReport));
    }

    /**
     * 处理建议
     */
    @PutMapping("/jianyi")
    public AjaxResult editjianyi(@RequestBody HeEventReport heEventReport) {
        //获取登录人的userid
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        //处理人ID
        heEventReport.setHandingPersonId(sysUser.getUserId());
        //将处理人姓名添加到HandingPerson字段中
        heEventReport.setHandingPerson(sysUser.getNickName());
        //获取当前时间
        heEventReport.setCreationTime(DateUtils.getNowDate());
        String pin = sysUser.getNickName() + "处理了建议";
        //新建个流转表对象
        HeEventFlow heEventFlow = new HeEventFlow();
        //将分析表的reporteventid注入到flow表的reporteventid中
        heEventFlow.setReportEventId(heEventReport.getId());
        heEventFlow.setCreationTime(DateUtils.getNowDate());
        heEventFlow.setProcessStreamContent(pin);
        heEventFlowService.insertHeEventFlow(heEventFlow);
        return toAjax(heEventReportService.updateHeEventReport(heEventReport));
    }

    /**
     * 主任意见
     */
    @PutMapping("/zhurenyijian")
    public AjaxResult zhurenyijian(@RequestBody HeEventReport heEventReport) {
        //获取登录人的userid
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        //新建个流转表对象
        HeEventFlow heEventFlow = new HeEventFlow();
        //将分析表的reporteventid注入到flow表的reporteventid中
        heEventFlow.setReportEventId(heEventReport.getId());
        heEventFlow.setCreationTime(DateUtils.getNowDate());
        if (heEventReport.getNote8().equals("01")){
            String pin = sysUser.getNickName() + "同意该处理结果";
            heEventFlow.setProcessStreamContent(pin);
        }else if (heEventReport.getNote8().equals("02")){
            if (heEventReport.getNote9()==null){
                String pin = sysUser.getNickName() + "不同意该处理结果";
                heEventFlow.setProcessStreamContent(pin);
            }else if (heEventReport.getNote9()!=null){
                String pin = sysUser.getNickName() + "不同意该处理结果原因是"+heEventReport.getNote9();
                heEventFlow.setProcessStreamContent(pin);
            }
        }
        heEventFlowService.insertHeEventFlow(heEventFlow);
        return toAjax(heEventReportService.updateHeEventReport(heEventReport));
    }

    /**
     * 结案
     */
    @PutMapping("/jiean")
    public AjaxResult editjiean(@RequestBody HeEventReport heEventReport) {
        //获取登录人的userid
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        heEventReport.setUserId(sysUser.getUserId());
        //获取当前时间
        heEventReport.setCreationTime(DateUtils.getNowDate());
        String pin = sysUser.getNickName() + "结案了该事件";
        //新建个流转表对象
        HeEventFlow heEventFlow = new HeEventFlow();
        //将分析表的reporteventid注入到flow表的reporteventid中
        heEventFlow.setReportEventId(heEventReport.getId());
        heEventFlow.setCreationTime(DateUtils.getNowDate());
        heEventFlow.setProcessStreamContent(pin);
        heEventFlowService.insertHeEventFlow(heEventFlow);
        return toAjax(heEventReportService.updateHeEventReport(heEventReport));
    }
}
